//
//  HRNewFriendsRequestListViewController.m
//  Hear
//
//  Created by mac on 2017/10/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRNewFriendsRequestListViewController.h"
#import "HRFMDBDynamicNotifacationTool.h"
#import "HRNewFriendsRequestListCell.h"
#import "HRDynamicNotificationModel.h"
#import "HRDynamicViewModel.h"
#import "HRUpdataUserInfoModel.h"
#import "HRDynamicViewController.h"

@interface HRNewFriendsRequestListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *contentArr;
@property(nonatomic, strong)HRDynamicViewModel *viewModel;

@end

@implementation HRNewFriendsRequestListViewController
#pragma mark - Get and Set
- (HRDynamicViewModel *)viewModel{
    if (_viewModel == nil) {
        _viewModel = [[HRDynamicViewModel alloc] init];
    }
    return _viewModel;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *deleteButton = [UIButton buttonWithType:0];
    deleteButton.size = CGSizeMake(60, 40);
    [deleteButton setTitle:@"一键清空" forState:0];
    [deleteButton setTitleColor:[UIColor whiteColor] forState:0];
    deleteButton.titleLabel.font = CustomUIFont(14);
    [deleteButton addTarget:self action:@selector(deleteAllContentList) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:deleteButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.contentArr = [[HRFMDBDynamicNotifacationTool sharedInstance]selectContentOfType:@"10"];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 70;
    
    
    for (HRDynamicNotificationModel *model in self.contentArr) {
        [[HRFMDBDynamicNotifacationTool sharedInstance] updateSomeField:@"isRead" andValue:@"1" andID:model.ID];
    }
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.contentArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HRNewFriendsRequestListCell *cell = [HRNewFriendsRequestListCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    HRDynamicNotificationModel *model = self.contentArr[indexPath.row];
    if (model){
        cell.notiModel = self.contentArr[indexPath.row];
    }
    WeakSelf
    cell.agreeButtonBlock = ^(){
        if (![model.isFriend integerValue]){
            [weakSelf saveUserFollow:model];
        }
            
    };
    return cell;
}



- (void)saveUserFollow:(HRDynamicNotificationModel *)model{
    
    WeakSelf
    HRSaveUserFollowRequestModel *requestModel = [HRSaveUserFollowRequestModel new];
    requestModel.CurrentUserLocalId = USERID;
    requestModel.FollowedUserLocalId = model.messageID;
    requestModel.CreateTime = [self getCurrentTime];
    
    
    [self.viewModel saveUserFollow:requestModel isSeccess:^(BOOL isSeccess) {
        if (isSeccess){
            dispatch_async(dispatch_get_main_queue(), ^{
                model.isFriend = @"1";
                [weakSelf.tableView reloadData];
               [[HRFMDBDynamicNotifacationTool sharedInstance] updateSomeField:@"isFriend" andValue:@"1" andID:model.ID];
            });
        }
    }];
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete){
        HRDynamicNotificationModel *model = self.contentArr[indexPath.row];
        [[HRFMDBDynamicNotifacationTool sharedInstance] deleteRequestId:model.ID];
        [self.contentArr removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRow:indexPath.row inSection:0 withRowAnimation:UITableViewRowAnimationFade];

    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    HRDynamicViewController *VC = [HRDynamicViewController new];
    HRDynamicNotificationModel *model = self.contentArr[indexPath.row];
    VC.currentUserID = model.messageID;
    [self.navigationController pushViewController:VC animated:YES];
    
}

#pragma mark 清空回话列表
- (void)deleteAllContentList{
    for (HRDynamicNotificationModel *model in self.contentArr) {
        [[HRFMDBDynamicNotifacationTool sharedInstance] deleteRequestId:model.ID];
    }
    [self.contentArr removeAllObjects];
    [self.tableView reloadData];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTabItemBageCountNotifi" object:nil];
}

- (NSString *)getCurrentTime
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    return [formatter stringFromDate:[NSDate date]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
